/* CsJni - .NET / Java Bridge
 * 
 * Copyright 2010 Robert E. Burke Licensed under the Apache License, Version 
 * 2.0 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at 
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable 
 * law or agreed to in writing, software distributed under the License is 
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
 * KIND, either express or implied. See the License for the specific language 
 * governing permissions and limitations under the License. 
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

namespace CsJni.PlatformAbstraction
{
    internal class Platform : IPlatform
    {
        private string mSearchForPathBase;
        private JavaVMConfiguration mCfg;

        protected Platform(JavaVMConfiguration cfg) 
        {
            mCfg = cfg;
            Assembly entryAsm = System.Reflection.Assembly.GetEntryAssembly(); // will be null when run from NUnit
            mSearchForPathBase = Path.GetDirectoryName((entryAsm != null) ? entryAsm.Location : System.Reflection.Assembly.GetExecutingAssembly().Location);
        }

        protected JavaVMConfiguration Cfg
        {
            get
            {
                return mCfg;
            }
        }

        public virtual string PlatformName 
        {
            get { return "null"; }
        }

        public virtual string GetCreatedJavaVMsMethodName { get { return "JNI_GetCreatedJavaVMs"; } }
        public virtual string CreateJavaVMMethodName { get { return "JNI_CreateJavaVM"; } }

        public virtual void FaultVfPrintf()
        {
            throw new NotSupportedException();
        }
        public virtual string ExtractVfprintf(IntPtr filePointer, IntPtr format, IntPtr arguments)
        {
            throw new NotSupportedException();
        }
        public virtual IntPtr LoadDynamicLibrary(string fileName)
        {
            throw new NotSupportedException();
        }
        public virtual void FreeDynamicLibrary(IntPtr libraryHandle)
        {
            throw new NotSupportedException();
        }
        public virtual T GetDelegateForLibraryMethod<T>(IntPtr libraryHandle, string methodName)
        {
            throw new NotSupportedException();
        }
        public virtual string FindJvmLibraryWithPath(string path)
        {
            return null;
        }
        public virtual string LookupJvmLibrary(string version)
        {
            return null;
        }
        public virtual string ExpandPath(string inputPath, ExpandPathType expandPathType)
        {
            string outputPath = NormalizePathDirectorySeparators(inputPath);
            if (!Path.IsPathRooted(outputPath))
            {   // NOTE: relative paths assumed to be relative to SearchForPathBase
                outputPath = Path.Combine(this.SearchForPathBase, outputPath);
            }
            outputPath = Path.GetFullPath(outputPath);
            return outputPath;
        }
        public virtual void Release()
        {
            // do nothing
        }
        public string NormalizePathDirectorySeparators(string inputPath)
        {
            return inputPath.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);
        }
        protected string ExpandAndVerifyPath(string inputPath, ExpandPathType expandPathType, bool throwIfDoesNotExist)
        {
            string outputPath = ExpandPath(inputPath, expandPathType);
            if (!File.Exists(outputPath))
            {
                if (throwIfDoesNotExist)
                {
                    throw new JniException(JniResourceManager.Instance.GetString("jvmPathFindFilesDoesNotExist"));
                }
                else
                {
                    return null;
                }
            }
            return outputPath;
        }
        protected string SearchForPathBase
        {
            get
            {
                return mSearchForPathBase;
            }
        }
    }
}
